package com.easonji.admin.controller;

import javax.annotation.Resource;
import javax.validation.Valid;

import com.easonji.admin.manager.DeptManager;
import com.easonji.admin.query.DeptQuery;
import com.easonji.core.annotion.BussinessLog;
import com.easonji.core.annotion.Permission;
import com.easonji.core.log.LogObjectHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.easonji.admin.form.DeptForm;
import com.easonji.admin.service.DeptService;
import com.easonji.common.base.controller.BaseController;
import com.easonji.common.base.response.Response;
import com.easonji.common.base.response.enums.ResponseEnum;
import com.easonji.common.constant.dictmap.DeptDict;
import com.easonji.common.util.StringUtils;

/**
 * @ClassName: DeptController
 * @Description: 部门控制器
 * @Author: LiuJun
 * @date: 2018/07/30 14:06:51
 * @Version: 1.0
 * @Copyright: Copyright (c) 2018 - 2019
 */
@Controller
@RequestMapping("/dept")
public class DeptController extends BaseController {

    /**
     * 页面前缀
     */
    private final static String PREFIX = "system/dept/";

    @Resource
    private DeptService deptService;

    @Resource
    private DeptManager deptManager;

    /**
     * 跳转到部门首页
     */
    @Permission
    @GetMapping("")
    public String index() {
        return PREFIX + "dept";
    }

    /**
     * 跳转到添加部门页面
     */
    @Permission
    @GetMapping("/to_add")
    public String toAdd() {
        return PREFIX + "dept_add";
    }

    /**
     * 跳转到修改部门页面
     */
    @Permission
    @GetMapping("/to_update/{deptId}")
    public String toUpdate(@PathVariable Integer deptId, Model model) {
        Object result = deptService.queryDept(deptId);
        LogObjectHolder.me().set(result);
        model.addAttribute("dept", result);
        return PREFIX + "dept_edit";
    }

    /**
     * 查询部门列表
     */
    @ResponseBody
    @Permission
    @PostMapping("/list")
    public Object queryDeptList(DeptQuery deptQuery) {
        Object result = deptService.queryDeptList(deptQuery);
        return result;
    }

    /**
     * 添加部门
     */
    @ResponseBody
    @Permission
    @BussinessLog(value = "添加部门", key = "name", dict = DeptDict.class)
    @PostMapping("/add")
    public Response addDept(@Valid DeptForm deptForm, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return failure(ResponseEnum.PARAMETER_ERROR);
        }
        int result = deptService.saveDept(deptForm);
        if (result == 0) {
            return failure(ResponseEnum.DATABASE_ERROR);
        }
        return success(ResponseEnum.SAVE_SUCCESS);
    }

    /**
     * 修改部门
     */
    @ResponseBody
    @Permission
    @BussinessLog(value = "修改部门", key = "name", dict = DeptDict.class)
    @PostMapping("/update")
    public Response updateDept(@Valid DeptForm deptForm, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return failure(ResponseEnum.PARAMETER_ERROR);
        }
        int result = deptService.updateDept(deptForm);
        if (result == 0) {
            return failure(ResponseEnum.DATABASE_ERROR);
        }
        return success(ResponseEnum.UPDATE_SUCCESS);
    }

    /**
     * 删除部门
     */
    @ResponseBody
    @Permission
    @BussinessLog(value = "删除部门", key = "name", dict = DeptDict.class)
    @PostMapping("/remove")
    public Response removeDept(@RequestParam Integer deptId) {
        if (StringUtils.isEmpty(deptId)) {
            return failure(ResponseEnum.PARAMETER_ERROR);
        }
        LogObjectHolder.me().set(deptManager.getDeptNameById(deptId));
        deptService.removeDept(deptId);
        return success(ResponseEnum.REMOVE_SUCCESS);
    }
}
